// app.js
App({
	onLaunch: function() {
		// 从缓存获取主题设置，默认为 'light'
		// const theme = wx.getStorageSync('theme') || 'light';
		this.globalData.theme = theme;
	},
	globalData: {
		theme: 'light',
		tabBarThemes: {
			light: {
				backgroundColor: '#FFFFFF',
				color: '#000000',
				selectedColor: '#1AAD19',
			},
			vibrant: {
				backgroundColor: '#ff9966',
				color: '#FFFFFF',
				selectedColor: '#ff5e62',
			},
			dark: {
				backgroundColor: '#1A1A1D',
				color: '#C5C6C7',
				selectedColor: '#FDD835',
			},
			nature: {
				backgroundColor: '#56ab2f',
				color: '#a8e063',
				selectedColor: '#3C3C3B',
			},
			ocean: {
				backgroundColor: '#2b5876',
				color: '#4e4376',
				selectedColor: '#FFFFFF',
			},
			retro: {
				backgroundColor: '#7b9acc',
				color: '#F4F4F8',
				selectedColor: '#5C5470',
			},
			elegant: {
				backgroundColor: '#B8A9C9',
				color: '#5C5470',
				selectedColor: '#5C5470',
			},
			misty: {
				backgroundColor: '#BDC3C7',
				color: '#2C3E50',
				selectedColor: '#ECECEC',
			},
			// ...更多的主题可以在这里添加
		},
	},
	applyTheme: function(theme) {
		// 设置 tabBar 的样式
		const tabBarStyle = this.globalData.tabBarThemes[theme];
		if (tabBarStyle) {
			wx.setTabBarStyle({
				backgroundColor: tabBarStyle.backgroundColor,
				color: tabBarStyle.color,
				selectedColor: tabBarStyle.selectedColor,
				success: function() {
					console.log('TabBar style updated to theme:', theme);
				},
				fail: function(err) {
					console.error('Failed to update TabBar style:', err);
				}
			});
			this.globalData.theme = theme; // 更新全局 theme
		}
	},
	// 更改主题并更新缓存
	// toggleTheme: function() {
	// 	let newTheme = this.globalData.theme === 'light' ? 'vibrant' : 'light';
	// 	this.globalData.theme = newTheme;
	// 	wx.setStorageSync('theme', newTheme);

	// 	// 通知所有页面更改主题
	// 	if (this.themeChangeCallback) {
	// 		this.themeChangeCallback(newTheme);
	// 	}
	// 	// 更新tabBar样式
	// 	const tabBarTheme = this.globalData.theme === 'light' ? {
	// 		backgroundColor: '#FFFFFF',
	// 		color: '#000000',
	// 		selectedColor: '#1AAD19'
	// 	} : {
	// 		backgroundColor: '#ff9966',
	// 		color: '#FFFFFF',
	// 		selectedColor: '#ff5e62'
	// 	};
	// 	wx.setTabBarStyle(tabBarTheme);
	// },

	toggleTheme: function() {
		// 定义所有可能的主题
		const themes = ['light', 'vibrant', 'dark', 'nature', 'ocean', 'retro', 'elegant', 'misty'];
		let currentThemeIndex = themes.indexOf(this.globalData.theme);
		let newThemeIndex = (currentThemeIndex + 1) % themes.length; // 循环数组索引
		let newTheme = themes[newThemeIndex];

		this.globalData.theme = newTheme;
		wx.setStorageSync('theme', newTheme);

		// 通知所有页面更改主题
		if (this.themeChangeCallback) {
			this.themeChangeCallback(newTheme);
		}

		// 更新tabBar样式，这里只是一个示例，您可能需要根据不同的主题设置不同的样式

		// 获取当前主题的 tabBar 样式
		this.applyTheme(newTheme);
	},
	themeChangeCallback: null // 保存主题变化回调
});